import pygame

from Scoreboard import Scoreboard
from button import Button
from game_state import GameState
from settings import Settings
from alien import Alien
from ship import Ship
import game_function as gf
from pygame.sprite import Group


def run_game():
    """创建一个游戏窗"""
    pygame.init()

    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    ship = Ship(ai_settings, screen)
    pygame.display.set_caption("Alien Invasion")
    bullets = Group()
    alients = Group()
    game_state = GameState(ai_settings);
    gf.create_feet(ai_settings, screen, alients, ship);
    play_button = Button(screen, "START")
    score_board = Scoreboard(game_state,ai_settings,screen);
    pygame.mixer.init()
    pygame.mixer.music.load("music/woman.mp3");

    while True:
        # 消息循环，接收键盘和鼠标事件
        gf.check_event(ship, ai_settings, screen, bullets,play_button,game_state,alients,score_board)

        if game_state.game_active:
            # 更新飞机位置
            ship.updatePosition()
            # 更新子弹的位置、清除子弹、新增子弹
            gf.update_bullet(bullets, alients, ai_settings, screen, ship, game_state, score_board)
            # 更新外星人的位置
            gf.update_alien(alients, bullets, ship, ai_settings, game_state, screen,score_board)

        # 重绘屏幕:绘制飞机、子弹
        gf.update_screen(screen, ship, ai_settings, bullets, alients, play_button, game_state,score_board)


run_game()
